<?php

class FantasyUser extends User {
	
	public $postava;
	public $kapiteni;
	
	public $poeni;
	
	public $ukupnoPoena;
	
	// constructor
	
	public function __construct($i = 0) {
		$this->id = $i;
		if ($i != 0) {
			parent::__construct($this->id);
		}
	}
	
	// functions
		
	public function getRoster($kolo) {
		$q = "SELECT pg, sg, sf, pf, c, capt FROM fts_tim WHERE user = ". $this->id ." AND kolo = ". $kolo;
		$r = mysql_query("set names 'utf8'");
		$r = mysql_query($q);
		if (mysql_num_rows($r)) {
			while ($n = mysql_fetch_array($r)) {
				$this->postava[$kolo]["pg"] = new Player($n['pg']);
				$this->postava[$kolo]["sg"] = new Player($n['sg']);
				$this->postava[$kolo]["sf"] = new Player($n['sf']);
				$this->postava[$kolo]["pf"] = new Player($n['pf']);
				$this->postava[$kolo]["c"] = new Player($n['c']);
				$this->kapiteni[$kolo] = new Player($n['capt']);
			}
		}
	}
	
	public function getSeasonRoster() {
		$liga = new Liga(1);
		$liga->getRounds();
		foreach($liga->rounds as $round) {
			$this->getRoster($round->id);
		}
	}
	
	public function findMatchForRound($kolo, $igrac) {/* pomocna funkcija */
		// nalazenje meca
		$q = "SELECT id FROM utakmica WHERE kolo = ". $kolo ." AND (domacin = ". $igrac->getTeamInfo()->id ." OR gost = ". $igrac->getTeamInfo()->id .")";
		$r = mysql_query($q);
		$n = mysql_fetch_array($r);
		return ($n['id']);
	}
	
	public function getStats($kolo) {
		foreach($this->postava[$kolo] as $igrac) {
			$igrac->getStats($this->findMatchForRound($kolo, $igrac));
			$igrac->achievements = new Achievement($igrac->id, $this->findMatchForRound($kolo, $igrac));
		}
	}
	
	public function getAllStats() {
		if ($this->postava) {
			foreach($this->postava as $kolo=>$tim) {
				$this->getStats($kolo);
			}
		}
	}
	
	public function calculatePoints($kolo) {
		$this->poeni[$kolo] = 0;
		if(isset($this->postava[$kolo])) {
			foreach($this->postava[$kolo] as $player) {
				$k = 1;
				if ($player->id == $this->kapiteni[$kolo]->id) $k = 2;
				$this->poeni[$kolo] += $k*$player->stats[$this->findMatchForRound($kolo, $player)]->BLindex;
			}
		}
	}
	
	public function calculateTotalPoints() {
		if ($this->postava) {
			foreach($this->postava as $kolo=>$tim) {
				$this->calculatePoints($kolo);
			}
			$this->ukupnoPoena = 0;
			if ($this->poeni) {
				foreach($this->poeni as $poen) {
					$this->ukupnoPoena += $poen;
				}
			}
		}
	}
	
	public function transferPlayer($position, $player, $round) {
		$q = "UPDATE `fts_tim` SET `".$position."` = ".$player." WHERE `user` = ".$this->id." AND `kolo` = ".$round;
		if (mysql_query($q)) {
			// echo "You have changed your lineup.";
		}
		else {
			"nije uspeo transfer";
		}
	}
	
	public function generateRandomTeam() {
		$arr = array("pg", "sg", "sf", "pf", "c");
		foreach($arr as $arg) {
			$q = "SELECT igrac FROM pozicije WHERE pozicija = '" . $arg ."' ORDER BY RAND() LIMIT 1";
			$r = mysql_query($q);
			$n = mysql_fetch_array($r);
			$this->postava[$arg] = new Player($n['igrac']);
		}
			$this->kapiteni = $this->postava["pg"];
	}

	// admin
	
	public function insertTeamIntoDb($round) {
		$q = "INSERT INTO fts_tim (user, kolo, pg, sg, sf, pf, c, capt) VALUES ('".$this->id."', '".$round."', '".$this->postava['pg']->id
		."', '".$this->postava['sg']->id."', '".$this->postava['sf']->id."', '".$this->postava['pf']->id."', '".$this->postava['c']->id."', '".
		$this->kapiteni->id."')";
		$r = mysql_query("set names 'utf8'");
		if (mysql_query($q)) {
			//echo "Random tim je dodat u bazu podataka";
		}
	}
				
} // end of class FantasyUser


?>